<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        :selected-keys="selectedKeys"
        @menu-item-click="doMenuClick"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginRight: '38px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/oj-logo.jpg" />
            <div class="title">月月鸟 OJ</div>
          </div>
        </a-menu-item>
        <!--导航栏-->
        <a-menu-item v-for="item in visibleRoutes" :key="item.path">
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <a-dropdown @select="handleSelect">
        <a-avatar
          v-if="!store.state.user?.loginUser?.userName"
          :style="{ backgroundColor: '#3370ff' }"
          >{{ "未登录" }}
        </a-avatar>
        <a-avatar v-else>
          <img alt="avatar" :src="store.state.user?.loginUser?.userAvatar" />
        </a-avatar>
        <template #content>
          <a-doption
            v-if="loginUserName"
            @click="() => handleSelect('profile')"
          >
            <template #icon>
              <icon-user />
            </template>
            <template #default>个人信息</template>
          </a-doption>
          <a-doption v-if="loginUserName" @click="() => handleSelect('logout')">
            <template #icon>
              <icon-export />
            </template>
            <template #default>退出登录</template>
          </a-doption>
          <!-- 如果用户未登录，则显示立即登录 -->
          <a-doption v-else @click="() => handleSelect('login')">
            <template #icon>
              <icon-import />
            </template>
            <template #default>立即登录</template>
          </a-doption>
        </template>
      </a-dropdown>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "../router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useStore } from "vuex";
import checkAccess from "@/access/checkAccess";
import AccessEnum from "@/access/accessEnum";
import { User, UserControllerService } from "../../generated";
import message from "@arco-design/web-vue/es/message";

const store = useStore();
const router = useRouter();
let loginUserName = "";

const handleSelect = async (action) => {
  if (action === "logout") {
    try {
      await UserControllerService.userLogoutUsingPost(); // 调用你的退出登录接口
      message.success("退出登录成功");
      router.push("/");
      // todo 退出登录后续处理
    } catch (error) {
      console.error("退出登录失败:", error);
      alert("退出登录失败，请重试");
    }
  } else if (action === "profile") {
    // 处理个人信息的逻辑
  } else if (action === "login") {
    router.push("/user/login");
  }
};

// 展示在菜单的路由数组
const visibleRoutes = computed(() => {
  return routes.filter((item, index) => {
    if (item.meta?.hideInMenu) {
      return false;
    }
    // 根据权限过滤菜单
    if (
      !checkAccess(store.state.user.loginUser, item?.meta?.access as string)
    ) {
      return false;
    }
    return true;
  });
});

setTimeout(() => {
  // store.dispatch("user/getLoginUser", {
  //   userName: "xxxx",
  //   userRole: AccessEnum.ADMIN,
  // });
  console.log("用户信息,姓名", store.state.user?.loginUser?.userName);
  loginUserName = store.state.user?.loginUser?.userName;
  if (loginUserName === "未登录") {
    loginUserName = "";
  }
  console.log("用户名  ====> ", loginUserName);
}, 1000);

// 默认主页
const selectedKeys = ref(["/"]);

// 路由更新后 更新选中的菜单项 进行高亮显示
router.afterEach((to, from, failure) => {
  selectedKeys.value = [to.path];
});

const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: #444;
  margin-left: 16px;
}

.logo {
  height: 48px;
}
</style>
